package com.evan.review.lifecycle;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.evan.review.R;

/**
 * Activity生命周期演示页面
 * 用于展示各种生命周期方法的调用情况
 */
public class LifecycleActivity extends AppCompatActivity implements View.OnClickListener {

    private static final String TAG = "LifecycleActivity";
    private TextView tvLifecycleInfo;
    private StringBuilder lifecycleLog = new StringBuilder();
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_lifecycle);
        
        logLifecycleEvent("onCreate() - savedInstanceState: " + (savedInstanceState == null ? "null" : "not null"));
        
        if (savedInstanceState != null) {
            // 从保存的状态恢复数据
            String savedLog = savedInstanceState.getString("lifecycle_log");
            if (savedLog != null) {
                lifecycleLog = new StringBuilder(savedLog);
                logLifecycleEvent("Restored from savedInstanceState");
            }
        }
        
        initViews();
        updateLifecycleInfo();
    }
    
    private void initViews() {
        tvLifecycleInfo = findViewById(R.id.tv_lifecycle_info);
        
        Button btnStandard = findViewById(R.id.btn_start_standard);
        Button btnDialog = findViewById(R.id.btn_start_dialog);
        Button btnTransparent = findViewById(R.id.btn_start_transparent);
        Button btnConfigChange = findViewById(R.id.btn_config_change);
        Button btnVMDemo = findViewById(R.id.btn_vm_demo);
        Button btnClear = findViewById(R.id.btn_clear_log);
        
        btnStandard.setOnClickListener(this);
        btnDialog.setOnClickListener(this);
        btnTransparent.setOnClickListener(this);
        btnConfigChange.setOnClickListener(this);
        btnVMDemo.setOnClickListener(this);
        btnClear.setOnClickListener(this);
    }
    
    @Override
    public void onClick(View v) {
        int id = v.getId();
        
        if (id == R.id.btn_start_standard) {
            // 启动普通Activity，观察完整生命周期
            startActivity(new Intent(this, StandardLifecycleActivity.class));
        } else if (id == R.id.btn_start_dialog) {
            // 启动对话框样式Activity，观察onPause但不触发onStop
            startActivity(new Intent(this, DialogLifecycleActivity.class));
        } else if (id == R.id.btn_start_transparent) {
            // 启动透明Activity，观察onPause但不触发onStop
            startActivity(new Intent(this, TransparentLifecycleActivity.class));
        } else if (id == R.id.btn_config_change) {
            // 启动配置变更演示Activity
            startActivity(new Intent(this, ConfigChangeActivity.class));
        } else if (id == R.id.btn_vm_demo) {
            // 启动使用ViewModel的Activity
            startActivity(new Intent(this, ViewModelActivity.class));
        } else if (id == R.id.btn_clear_log) {
            // 清空日志
            lifecycleLog = new StringBuilder();
            updateLifecycleInfo();
        }
    }
    
    /**
     * 记录生命周期事件
     */
    private void logLifecycleEvent(String event) {
        Log.d(TAG, event);
        lifecycleLog.append(event).append("\n");
    }
    
    /**
     * 更新生命周期信息显示
     */
    private void updateLifecycleInfo() {
        tvLifecycleInfo.setText(lifecycleLog.toString());
    }
    
    @Override
    protected void onStart() {
        super.onStart();
        logLifecycleEvent("onStart()");
        updateLifecycleInfo();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        logLifecycleEvent("onResume()");
        updateLifecycleInfo();
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        logLifecycleEvent("onPause()");
        updateLifecycleInfo();
    }
    
    @Override
    protected void onStop() {
        super.onStop();
        logLifecycleEvent("onStop()");
        // 注意：在onStop后更新UI可能不会显示，因为Activity已不可见
    }
    
    @Override
    protected void onRestart() {
        super.onRestart();
        logLifecycleEvent("onRestart()");
        updateLifecycleInfo();
    }
    
    @Override
    protected void onDestroy() {
        super.onDestroy();
        logLifecycleEvent("onDestroy()");
        // 注意：此方法执行后UI不再可见
    }
    
    @Override
    protected void onSaveInstanceState(@NonNull Bundle outState) {
        super.onSaveInstanceState(outState);
        // 保存当前日志以便恢复
        outState.putString("lifecycle_log", lifecycleLog.toString());
        logLifecycleEvent("onSaveInstanceState()");
        updateLifecycleInfo();
    }
    
    @Override
    protected void onRestoreInstanceState(@NonNull Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
        logLifecycleEvent("onRestoreInstanceState()");
        updateLifecycleInfo();
    }
    
    @Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        logLifecycleEvent("onWindowFocusChanged() - hasFocus: " + hasFocus);
        updateLifecycleInfo();
    }
    
    @Override
    protected void onUserLeaveHint() {
        super.onUserLeaveHint();
        logLifecycleEvent("onUserLeaveHint() - 用户离开提示");
        updateLifecycleInfo();
    }
    
    @Override
    protected void onNewIntent(Intent intent) {
        super.onNewIntent(intent);
        logLifecycleEvent("onNewIntent() - " + intent.toString());
        updateLifecycleInfo();
    }
} 